package com.freedom.admin.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.admin.domain.repository.RoleRepository;
import com.freedom.admin.infrastructure.persistence.assembler.RoleAssembler;
import com.freedom.admin.infrastructure.persistence.mapper.SysRoleMapper;
import com.freedom.admin.infrastructure.persistence.pojo.bo.RoleBO;
import com.freedom.admin.infrastructure.persistence.pojo.bo.RolePageBO;
import com.freedom.admin.infrastructure.persistence.pojo.po.SysRolePO;
import com.freedom.admin.infrastructure.persistence.pojo.query.RolePageQuery;
import com.freedom.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class RoleRepositoryImpl implements RoleRepository {

    private final SysRoleMapper sysRoleMapper;

    private final RoleAssembler roleAssembler;

    @Override
    public Page<RolePageBO> listRolePages(RolePageQuery queryParams) {
        Page<RolePageBO> page = new Page<>(queryParams.getCurrent(), queryParams.getPageSize());
        boolean userRoot = SecurityUtils.isRoot();
        return sysRoleMapper.selectRolePages(page, userRoot, queryParams);
    }

    @Override
    public List<RoleBO> queryRoleList() {
        return sysRoleMapper.queryRoleList();
    }

    @Override
    public RoleBO findById(Integer roleId) {
        SysRolePO sysRolePO = sysRoleMapper.selectById(roleId);
        return roleAssembler.toBO(sysRolePO);
    }

    @Override
    public void add(RoleBO roleBO) {
        SysRolePO sysRolePO = roleAssembler.toPO(roleBO);
        sysRoleMapper.insert(sysRolePO);
    }

    @Override
    public void modifyRole(RoleBO roleBO) {
        SysRolePO sysRolePO = roleAssembler.toPO(roleBO);
        sysRoleMapper.updateById(sysRolePO);
    }

    @Override
    public void removeRoles(List<Integer> roleIds) {
        sysRoleMapper.deleteBatchIds(roleIds);
    }

    @Override
    public Boolean modifyRoleStatus(RoleBO roleBO) {
        return sysRoleMapper.updateRoleStatus(roleBO.getId(), roleBO.getStatus());
    }
}
